/*
 ============================================================================
 Name		: BlockSetFirstPage.cpp
 Author	  : Zhang Jiawei
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CBlockSetFirstPage implementation
 ============================================================================
 */

#include "BlockSetFirstPage.h"
#include "BlockSetAP.h"
#include "BlockSetFont.h"
#include "BlockSetNetWork.h"
#include "eStockappui.h"
#include "ConfigFile.h"
#include "FolderUtils.h"
#include <eStock_L2hy.rsg>

CBlockSetFirstPage::CBlockSetFirstPage(CBaseBlock* bsBack):
CBlock(bsBack)
{
}

CBlockSetFirstPage::~CBlockSetFirstPage()
{
    if(m_Options != NULL)
        m_Options->Reset(), delete m_Options;
}

CBlockSetFirstPage* CBlockSetFirstPage::NewLC(CBaseBlock* bsBack)
{
    CBlockSetFirstPage* self = new (ELeave) CBlockSetFirstPage(bsBack);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

CBlockSetFirstPage* CBlockSetFirstPage::NewL(CBaseBlock* bsBack)
{
    CBlockSetFirstPage* self = CBlockSetFirstPage::NewLC(bsBack);
    CleanupStack::Pop(); // self;
    return self;
}

void CBlockSetFirstPage::ConstructL()
{
    CBlock::ConstructL();
    m_nCurrent = m_pGlobal->g_GoodFirstPage;
    m_nSelected = m_pGlobal->g_GoodFirstPage;
    
    CGlobal::GetDesFromID(iTip, LOC_BUF_STR_SETFIRSTPAGETIP);
    
    m_Options = new(ELeave)CDesCArraySeg(4);
    TBuf<16> option;
    CGlobal::GetDesFromID(option, LOC_BUF_STR_FENSHITU);
    m_Options->AppendL(option);
    CGlobal::GetDesFromID(option, LOC_BUF_STR_CAOPANXIAN);
    m_Options->AppendL(option);
    
    CGlobal::GetDesFromID(m_strTitle, LOC_BUF_MENU_SETTING);
}

void CBlockSetFirstPage::OnDraw(CFbsBitGc& g) const
{
    TRect rect;
    TInt nStart = m_nCurrent / m_nLinesPerPage * m_nLinesPerPage;
    TInt nX = m_nLineHeight + 3;
    
    CGlobal::DrawString(g, iTip, 3, m_rect.iTl.iY, TOP|LEFT, m_pGlobal->g_rgbHighlight);

    for (TInt i = 0; i < m_nLinesPerPage; i++)
    {
        TInt nPos = nStart + i;
        if (nPos>=m_Options->MdcaCount())
            break;
        
        TInt nY = m_rect.iTl.iY + m_nLineHeight * (i + 1);

        if (m_nCurrent == nPos)
            DrawImageRect(g,0, nY, m_rect.Width(), nY+m_nLineHeight,1);

        g.SetPenColor(m_pGlobal->g_rgbText);
        g.SetBrushStyle(CGraphicsContext::ENullBrush);
        rect.SetRect(TPoint(2,nY + m_pGlobal->m_nSpaceUp),TSize(m_pGlobal->g_nFontHeight,m_pGlobal->g_nFontHeight));
        g.DrawEllipse(rect);
        g.SetBrushStyle(CGraphicsContext::ESolidBrush);

        if(nPos == m_nSelected)
        {
            rect.Shrink(4,4);
            g.SetBrushColor(m_pGlobal->g_rgbText);
            g.DrawEllipse(rect);
        }

        CGlobal::DrawString(g, m_Options->MdcaPoint(nPos), nX, nY, TOP|LEFT, m_pGlobal->g_rgbText);
    }
}

void CBlockSetFirstPage::OnKeyDown(TInt keyCode)
{
    if (keyCode==m_pGlobal->g_nKeyCancel)
    {
    	if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
        	eStockUI->GetCurView()->iContainer->DelBlock();
        return;
    }
    if (keyCode==m_pGlobal->g_nKeyOK || keyCode==EStdKeyNkpAsterisk || keyCode == '*')
    {
        m_pGlobal->g_GoodFirstPage = m_nSelected;
        
        TBuf<1> firstpage;
        firstpage.AppendNum(m_pGlobal->g_GoodFirstPage);
        TConfigFile::WriteIniL(_L("googfirstpage"), firstpage, FolderUtils::ConfigDataFile());
        if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
        	eStockUI->GetCurView()->iContainer->DelBlock();
        return;
    }
    if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
    {
        TInt nLine = m_Options->MdcaCount();
        if (nLine>0)
        {
            TInt nOldCurrent = m_nCurrent;
            if (keyCode==m_pGlobal->g_nKeyUp)
            {
                m_nCurrent--;
                if (m_nCurrent < 0)
                {
                    m_nCurrent = 0;
                    iTabbar->SetFocus(ETrue);
                    DrawNow();
                }
            }
            else if (keyCode==m_pGlobal->g_nKeyDown)
            {
                m_nCurrent++;
                if (m_nCurrent > nLine-1)
                {
                    m_nCurrent = nLine-1;
                    iToolbar->SetFocus(ETrue);
                    DrawNow();
                }
            }
            if (nOldCurrent!=m_nCurrent)
            {
                if (m_bShowScroll)
                    m_sbVert->SetPos(m_nCurrent);
                DrawNow();
            }
        }
        return;
    }
    if (keyCode==m_pGlobal->g_nKeySelect)
    {
        TInt nLine = m_Options->MdcaCount();
        if (m_nCurrent >= 0 && m_nCurrent < nLine)
        {
            if(m_nSelected != m_nCurrent)
            {
                m_nSelected = m_nCurrent;
                DrawNow();
            }
        }
        return;
    }
}

void CBlockSetFirstPage::OnMouseUp(const TPoint& aPt)
{
    TInt nPos = (aPt.iY - m_rect.iTl.iY - m_nLineHeight)/m_nLineHeight;
    if (nPos>=0&&nPos<m_nLinesPerPage)
    {
        nPos += m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;
        if (nPos < m_Options->MdcaCount())
        {
            if (m_nCurrent!=nPos)
            {
                m_nCurrent = nPos;
                if (m_bShowScroll)
                    m_sbVert->SetPos(m_nCurrent);
            }
            else
            {
                OnKeyEvent(m_pGlobal->g_nKeySelect);
            }
            DrawNow();
        }
    }
}

void CBlockSetFirstPage::SizeChanged()
{
    CBlock::SizeChanged();
    iToolbar->SetSoftKey(CToolbar::EOK, CToolbar::EBack);
    iTabbar->SetSelectedId(KTabSettingFirstPage);
}

void CBlockSetFirstPage::AddTabs()
{
    TBuf<8> label;
    CGlobal::GetDesFromID(label,LOC_BUF_STR_APSET);
    iTabbar->AppendTab(label, KTabSettingAp);
    CGlobal::GetDesFromID(label,LOC_BUF_MENU_TYPEFACE);
    iTabbar->AppendTab(label, KTabSettingFont);
    CGlobal::GetDesFromID(label,LOC_BUF_STR_FRESHSET);
    iTabbar->AppendTab(label, KTabSettingFresh);
    /*CGlobal::GetDesFromID(label,LOC_BUF_STR_FIRSTPAGESET);
    iTabbar->AppendTab(label, KTabSettingFirstPage);*/
}

void CBlockSetFirstPage::BeforeDraw(CFbsBitGc& g) const
{
    iTitlebar->SetTitleText(m_strTitle);
    CBlock::BeforeDraw(g);
}

void CBlockSetFirstPage::HandleTabbarCommand(TInt aCommand)
{
    CBlock* block = NULL;
    switch (aCommand)
    {
    case KTabSettingFont:
        block = CBlockSetFont::NewL(m_pBlockBack);
        break;
    case KTabSettingAp:
        block = CBlockSetAP::NewL(m_pBlockBack);
        break;
    case KTabSettingFresh:
        block = CBlockSetNetWork::NewL(m_pBlockBack);
        break;
    case KTabSettingFirstPage:
        block = CBlockSetFirstPage::NewL(m_pBlockBack);
        break;
    default:
        break;
    }
    if (block)
        GetContainer()->AddBlock(block);
}
